fn main() {
    println!("Hello, world!");
    let ans = Solution::letter_combinations("23456789".to_string());
    println!("{:?}", ans);
    let ans = Solution::letter_combinations("7".to_string());
    println!("{:?}", ans);
}

struct Solution;

impl Solution {
    pub fn letter_combinations(digits: String) -> Vec<String> {
        let mut ans = Vec::new();
        if digits.is_empty() {
            return ans;
        }
        let get_len = |c: char| -> (usize, usize, [char; 4]) {
            match c {
                '2' => (0, 3, ['a', 'b', 'c', ' ']),
                '3' => (0, 3, ['d', 'e', 'f', ' ']),
                '4' => (0, 3, ['g', 'h', 'i', ' ']),
                '5' => (0, 3, ['j', 'k', 'l', ' ']),
                '6' => (0, 3, ['m', 'n', 'o', ' ']),
                '7' => (0, 4, ['p', 'q', 'r', 's']),
                '8' => (0, 3, ['t', 'u', 'v', ' ']),
                '9' => (0, 4, ['w', 'x', 'y', 'z']),
                _   => panic!(),
            }
        };
        let mut list = Vec::new();
        for c in digits.chars() {
            list.push(get_len(c));
        }
        let len = list.len();
        let mut flag = false;
        loop {
            let mut s = String::new();
            for elem in &list {
                let index = elem.0;
                let chars = elem.2;
                s.push(chars[index]);
            }
            ans.push(s);
            let mut bit = 1;
            for i in 0..len {
                let elem = &mut list[len - i - 1];
                let fur = elem.0 + bit;
                if fur >= elem.1 {
                    bit = 1;
                    elem.0 = 0;
                    if i == len - 1 {
                        flag = true;
                        break;
                    }
                } else {
                    elem.0 = elem.0 + 1;
                    break;
                }
            }
            if flag {
                break;
            }
        }
        ans
    }
}
